﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using System.IO;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Graphics;
#endregion

using TImport = Microsoft.Xna.Framework.Content.Pipeline.Graphics.TextureContent;
using Color = Microsoft.Xna.Framework.Graphics.Color;
using Rectangle = System.Drawing.Rectangle;

#region LICENSE

/*
    This file is part of starLiGHT.Content.

    starLiGHT.Content is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    starLiGHT.Content is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with starLiGHT.Content.  If not, see <http://www.gnu.org/licenses/>.

    
    ADDITIONAL (commercial) LICENSES for starLiGHT.Content are available on request.
*/

#endregion

#region Version Stuff
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 1514                  $:  Revision of last commit *
// * $Author:: glatzemann         $:  Author of last commit   *
// * $Date:: 2010-10-12 10:12:09 #$:  Date of last commit     *
// ************************************************************
#endregion

namespace starLiGHT.Content
{
    [ContentImporter(".gif", DisplayName = "GIF Image Importer - starLiGHT.Engine", DefaultProcessor = "Texture")]
    public unsafe class GIFImporter : ContentImporter<TImport>
    {
        public override TImport Import(string filename, ContentImporterContext context)
        {
            var content = new Texture2DContent
            {
                Identity = new ContentIdentity(new FileInfo(filename).FullName, "GIF Image Importer - starLiGHT.Engine")
            };

            using (Image img = Image.FromFile(filename))
            {
                Rectangle bounds = new Rectangle(0, 0, img.Width, img.Height);

                // Create a copy of the image in 32bit ARGB format
                Bitmap copy = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);

                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(copy))
                {
                    g.PageUnit = GraphicsUnit.Pixel;
                    g.DrawImageUnscaled(img, bounds);
                }

                BitmapData tt = copy.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                byte* data = (byte*)tt.Scan0.ToPointer();
                int index = 0;
                byte[] bytes = new byte[img.Width * img.Height * 4];

                for (int i = 0; i < img.Width; i++)
                {
                    for (int j = 0; j < img.Height; j++)
                    {
                        for (int k = 0; k < 4; k++)
                        {
                            bytes[index] = data[index];
                            index++;
                        }
                    }
                }

                copy.UnlockBits(tt);

                var bitmapContent = new PixelBitmapContent<Color>(img.Width, img.Height);
                bitmapContent.SetPixelData(bytes);

                content.Mipmaps.Add(bitmapContent);
            }

            return content;
        }
    }
}
